// DlgSetAccount.cpp : 实现文件
//
#include "AIPanelDataBroker.h"

#include <qbprotocol/include/SSQBModel.h>
#include <qbprotocol/include/rdProto.h>
#include <qbtools/message/MessagePoster.h>
#include <uam/UserAccountManager.h>

#include <QFont>
#include <QHBoxLayout>
#include <QLabel>
#include <QPixmap>
#include <QRect>
#include <QString>
#include <QStylePainter>
#include <QVBoxLayout>

#include "../StringResource.h"
#include "auth/MessageProcess.h"

///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// class CAIPanelDataBusinessTypePanel
class CAIPanelDataBusinessTypePanel : public QWidget {
 public:
  enum type {
    typeNone = 0,
    typeBond,
    typeIRS,
    typeNCD,
    typeNCD_P,
    typeKungfuBond,
    typeBill,
    typeInvalid
  };
  CAIPanelDataBusinessTypePanel(type nType, QWidget* parent = nullptr);
  virtual ~CAIPanelDataBusinessTypePanel();

  void SetParent(QWidget* pParent);
  void SetFont(QFont& lf);
  void Init();
  int GetHeight();
  void Draw(QPainter* painter, int nTop);
  void DrawButton(QPainter* painter, QString& szTitle, QRect& R, int status);
  void SetRect(QRect);
  int OnMouseMove(int x, int y);
  int OnLButtonUp(int x, int y);
  int OnLButtonDown(int x, int y);

 protected:
  type m_nType;
  QString m_strType;
  QWidget* m_pParent;
  QFont m_lf;
  SImage* m_imgPhone;
  SImage* m_imgMobile;
  SImage* m_imgEmail;

  std::map<int, SImage*> m_imgBroker;
  std::map<int, QRect> m_rcButtons;
  std::vector<int> m_stDisable;
  int m_nHeight;
  int m_nMousePos, m_nMouseDown;
  QRect m_rct;
};

CAIPanelDataBusinessTypePanel::CAIPanelDataBusinessTypePanel(type nType,
                                                             QWidget* parent)
    : QWidget(parent) {
  m_nType = nType;
  switch (m_nType) {
    case typeBond:
      m_strType = "BOND";
      break;
    case typeIRS:
      m_strType = "IRS";
      break;
    case typeNCD:
      m_strType = "NCD";
      break;
    case typeNCD_P:
      m_strType = "NCD_P.BROKER";
      break;
    case typeKungfuBond:
      m_strType = "KUNGFU_BOND";
      break;
    case typeBill:
      m_strType = "BILL";
      break;
    default:
      m_strType = "";
      break;
  }

  m_imgBroker[1] = ImgMgr::instance().image("guiframe_grid_broker_1_cn");
  m_imgBroker[2] = ImgMgr::instance().image("guiframe_grid_broker_2_cn");
  m_imgBroker[3] = ImgMgr::instance().image("guiframe_grid_broker_3_cn");
  m_imgBroker[4] = ImgMgr::instance().image("guiframe_grid_broker_4_cn");
  m_imgBroker[5] = ImgMgr::instance().image("guiframe_grid_broker_5_cn");
  m_imgBroker[6] = ImgMgr::instance().image("guiframe_grid_broker_6_cn");
  m_imgBroker[20] = ImgMgr::instance().image("guiframe_grid_broker_20_cn");
  m_imgBroker[98] = ImgMgr::instance().image("guiframe_grid_broker_98_cn");

  m_imgPhone = ImgMgr::instance().image("guiframe_icon_microphone");
  m_imgMobile = ImgMgr::instance().image("guiframe_icon_mobilephone");
  m_imgEmail = ImgMgr::instance().image("guiframe_icon_email");

  m_nMousePos = 0;
  m_nMouseDown = 0;
}

CAIPanelDataBusinessTypePanel::~CAIPanelDataBusinessTypePanel() {}

void CAIPanelDataBusinessTypePanel::SetParent(QWidget* pParent) {
  m_pParent = pParent;
}

void CAIPanelDataBusinessTypePanel::SetFont(QFont& lf) { m_lf = lf; }
void CAIPanelDataBusinessTypePanel::Init() {
  if (m_nType <= typeNone || m_nType >= typeInvalid) return;
  int nFontHeight = 40;
  int nCount = 0;
  {
    m_nHeight = 24 + 4;
    const CBrokerList& brokerList =
        SSUserAccountManager::instance().GetReadonlyBroker();
    for (CBrokerList::BrokerMap::const_iterator itor =
             brokerList.GetBrokerMap().begin();
         itor != brokerList.GetBrokerMap().end(); itor++) {
      const CBrokerUnit& stUnit = itor->second;
      if (!CBrokerList::IsValidBroker(stUnit.m_brokerID)) continue;
      if (stUnit.m_brokerID == 98) continue;
      if (stUnit.m_BusinessList.size() <= 0) continue;
      CBrokerList::BusinessList::const_iterator itBus =
          stUnit.m_BusinessList.begin();
      for (; itBus != stUnit.m_BusinessList.end(); itBus++) {
        if (strcmp(itBus->m_BusinessCode, m_strType.toStdString().c_str()) ==
            0) {
          nCount++;
          break;
        }
      }
    }
  }

  m_nHeight += nFontHeight * nCount;
}

void CAIPanelDataBusinessTypePanel::SetRect(QRect r) { m_rct = r; }

int CAIPanelDataBusinessTypePanel::GetHeight() { return m_nHeight; }

void CAIPanelDataBusinessTypePanel::Draw(QPainter* painter, int nTop) {
  QColor crHead = 0x47461A;
  QColor crName = NAME_COLOR("TextNormal");
  QColor crContect = NAME_COLOR("TextNormal");
  QColor crWaiting = NAME_COLOR("Highlight");
  QColor crComfirmed = NAME_COLOR("Down_Reduce_H1");
  QColor crInvalid = NAME_COLOR("SupportingWord");

  QString strTemp;
  switch (m_nType) {
    case typeBond:
      strTemp = NewStringTable::strAIPanelBusiness(0);
      break;  // 债券
    case typeIRS:
      strTemp = NewStringTable::strAIPanelBusiness(1);
      break;  // IRS
    case typeNCD:
      strTemp = NewStringTable::strAIPanelBusiness(2);
      break;  // NCD
    case typeNCD_P:
      strTemp = NewStringTable::strAIPanelBusiness(3);
      break;  // NCD一级
    case typeKungfuBond:
      strTemp = NewStringTable::strAIPanelBusiness(4);
      break;  // 中资美元债
    case typeBill:
      strTemp = NewStringTable::strAIPanelBusiness(5);
      break;  // 中资美元债
    default:
      return;
  }

  const int lineheight = 32;
  const int gap = 8;
  // 数据权限-经纪商
  int X = 24;
  int right = m_rct.right() - 24;
  int Y = nTop;

  painter->setPen(QPen(crContect));
  QFont font("Microsoft YaHei");
  font.setPixelSize(12);
  painter->setFont(font);
  QRect textRect = QRect(X, Y, right - X, 24);
  painter->drawText(textRect, strTemp, QTextOption(Qt::AlignLeft));

  Y += 28;
  int nState = 0;
  const CBrokerList& brokerList =
      SSUserAccountManager::instance().GetReadonlyBroker();

  int nameLength = 80;
  int nMaxWidth = 1000;

  for (CBrokerList::BrokerMap::const_iterator itor =
           brokerList.GetBrokerMap().begin();
       itor != brokerList.GetBrokerMap().end(); itor++) {
    const CBrokerUnit& stUnit = itor->second;
    if (!CBrokerList::IsValidBroker(stUnit.m_brokerID)) continue;
    if (stUnit.m_brokerID == 98) continue;
    if (stUnit.m_BusinessList.size() <= 0) continue;
    bool bValid = false;

    CBrokerList::BusinessList::const_iterator itBus =
        stUnit.m_BusinessList.begin();
    for (; itBus != stUnit.m_BusinessList.end(); itBus++) {
      if (strcmp(itBus->m_BusinessCode, m_strType.toStdString().c_str()) == 0) {
        nState = SSUserAccountManager::GetBussinessState(
            itBus->m_Status_C, itBus->m_Status_T, itBus->m_Status_B);

        std::map<int, SImage*>::iterator it =
            m_imgBroker.find(stUnit.m_brokerID);
        if (it == m_imgBroker.end() || !(it->second)) break;

        QRect rect = QRect(X, Y, right - X, lineheight);
        painter->setPen(QPen(NAME_COLOR("PartPlate")));
        painter->setBrush(QBrush(StyleMgr::instance().color("PartPlate")));
        painter->drawRoundedRect(rect, 2, 2);

        bValid = true;
        int x = X + gap;
        SImage* img = it->second;
        int y = Y + (lineheight - img->height()) / 2;
        QPixmap piximg = QPixmap::fromImage(*img);
        painter->drawPixmap(x, y, piximg);
        painter->setFont(m_lf);

        x += img->width() + gap;
        if (nState != SSUserAccountManager::typeApply) {  // 显示联系人
          if (itBus->m_BrokerList.size() <= 0) {
            strTemp = QString::fromLocal8Bit(itBus->m_ContactName).trimmed();
            QRect textRect = QRect(x, Y, nameLength, lineheight);
            painter->setPen(QPen(crName));
            painter->drawText(textRect, strTemp,
                              QTextOption(Qt::AlignLeft | Qt::AlignVCenter));
            x += nameLength + 1;
            if (itBus->m_ContactTEL[0] != '\0') {
              x += (24 - m_imgPhone->width()) / 2;
              y = Y + (lineheight - m_imgPhone->height()) / 2;
              QPixmap piximgPhone = QPixmap::fromImage(*m_imgPhone);
              painter->drawPixmap(x, y, piximgPhone);
              x += 24 + 1;

              strTemp = QString::fromLocal8Bit(itBus->m_ContactTEL).trimmed();
              QRect textRect = QRect(x, Y, nMaxWidth, lineheight);
              painter->setPen(QPen(crContect));
              painter->drawText(textRect, strTemp,
                                QTextOption(Qt::AlignLeft | Qt::AlignVCenter));
            } else if (itBus->m_Email[0] != '\0') {
              x += (24 - m_imgEmail->width()) / 2;
              y = Y + (lineheight - m_imgEmail->height()) / 2;
              QPixmap piximgEmail = QPixmap::fromImage(*m_imgPhone);
              painter->drawPixmap(x, y, piximgEmail);
              x += 24 + 1;

              strTemp = QString::fromLocal8Bit(itBus->m_Email).trimmed();
              QRect textRect = QRect(x, Y, nMaxWidth, lineheight);
              painter->setPen(QPen(crContect));
              painter->drawText(textRect, strTemp,
                                QTextOption(Qt::AlignLeft | Qt::AlignVCenter));
            }
          } else {
            std::list<xQBAuthorityBroker_c>::const_iterator itrB =
                itBus->m_BrokerList.begin();
            strTemp = QString::fromLocal8Bit(itrB->m_DisplayName).trimmed();
            QRect textRect = QRect(x, Y, nameLength, lineheight);
            painter->setPen(QPen(crName));
            painter->drawText(textRect, strTemp,
                              QTextOption(Qt::AlignLeft | Qt::AlignVCenter));

            x += nameLength + 1;

            if (itrB->m_TEL[0] != '\0') {
              x += (24 - m_imgPhone->width()) / 2;
              y = Y + (lineheight - m_imgPhone->height()) / 2;
              QPixmap piximgPhone = QPixmap::fromImage(*m_imgPhone);
              painter->drawPixmap(x, y, piximgPhone);
              x += 24 + 1;

              strTemp = QString::fromLocal8Bit(itrB->m_TEL).trimmed();
              QRect textRect = QRect(x, Y, nameLength, lineheight);
              painter->setPen(QPen(crContect));
              painter->drawText(textRect, strTemp,
                                QTextOption(Qt::AlignLeft | Qt::AlignVCenter));
            } else if (itrB->m_Mobile[0] != '\0') {
              x += (24 - m_imgMobile->width()) / 2;
              y = Y + (lineheight - m_imgMobile->height()) / 2;
              QPixmap piximgMobible = QPixmap::fromImage(*m_imgPhone);
              painter->drawPixmap(x, y, piximgMobible);
              x += 24 + 1;

              strTemp = QString::fromLocal8Bit(itrB->m_Mobile).trimmed();
              QRect textRect = QRect(x, Y, nameLength, lineheight);
              painter->setPen(QPen(crContect));
              painter->drawText(textRect, strTemp,
                                QTextOption(Qt::AlignLeft | Qt::AlignVCenter));
            } else if (itrB->m_Email[0] != '\0') {
              x += (24 - m_imgEmail->width()) / 2;
              y = Y + (lineheight - m_imgEmail->height()) / 2;
              QPixmap piximgEmail = QPixmap::fromImage(*m_imgPhone);
              painter->drawPixmap(x, y, piximgEmail);
              x += 24 + 1;

              strTemp = QString::fromLocal8Bit(itrB->m_Email).trimmed();
              QRect textRect = QRect(x, Y, nameLength, lineheight);
              painter->setPen(QPen(crContect));
              painter->drawText(textRect, strTemp,
                                QTextOption(Qt::AlignLeft | Qt::AlignVCenter));
            }
          }
        }

        switch (nState) {
          case SSUserAccountManager::typeApply:  // 有申请按钮
          {
            int nBtnWidth = 80;
            int nBtnHeight = 24;
            strTemp = NewStringTable::strAIPanelBusinessApply(0);  // 申请

            y = Y + (lineheight - nBtnHeight) / 2;
            QRect rc(right - 4 - nBtnWidth, y, nBtnWidth, nBtnHeight);
            m_rcButtons[stUnit.m_brokerID] = rc;
            if (itBus->m_bApplyed) {
              m_stDisable.insert(m_stDisable.end(), stUnit.m_brokerID);
              DrawButton(painter, strTemp, rc, 0);
            } else if (m_nMouseDown == stUnit.m_brokerID)
              DrawButton(painter, strTemp, rc, 1);
            else if (m_nMousePos == stUnit.m_brokerID)
              DrawButton(painter, strTemp, rc, 2);
            else
              DrawButton(painter, strTemp, rc, 3);
            break;
          }
          case SSUserAccountManager::typeApplyPass:  // 申请通过，显示联系人
          {
            strTemp = NewStringTable::strAIPanelBusinessApply(1);  // 已授权
            QFontMetrics fontMetrics(m_lf);
            int nFontWidth = fontMetrics.width(strTemp);
            int left = right - 16 - nFontWidth;
            QRect textRect = QRect(left, Y, nFontWidth, lineheight);
            painter->setPen(QPen(crComfirmed));
            painter->drawText(textRect, strTemp,
                              QTextOption(Qt::AlignLeft | Qt::AlignVCenter));

            SImage* img =
                ImgMgr::instance().image("guiframe_icon_tips_success");
            QPixmap m_tipssuccess = QPixmap::fromImage(*img);
            int x = left - 24 + (24 - img->height()) / 2;
            int y = Y + (lineheight - img->width()) / 2;
            painter->drawPixmap(x, y, m_tipssuccess);

            break;
          }
          case SSUserAccountManager::typeApplying:  // 审核中，显示联系人
          {
            strTemp = NewStringTable::strAIPanelBusinessApply(2);  // 申请中
            QFontMetrics fontMetrics(m_lf);
            int nFontWidth = fontMetrics.width(strTemp);
            int left = right - 16 - nFontWidth;
            QRect textRect = QRect(left, Y, nFontWidth, lineheight);
            painter->setPen(QPen(crWaiting));
            painter->drawText(textRect, strTemp,
                              QTextOption(Qt::AlignLeft | Qt::AlignVCenter));
            break;
          }
          case SSUserAccountManager::typeApplyNPass:  // 已拒绝，显示联系人
          case SSUserAccountManager::typeStop:  // 已停用，显示联系人
          case SSUserAccountManager::typeNull:  // 黑名单，显示联系人
          default: {
            strTemp = NewStringTable::strAIPanelBusinessApply(3);  // 无法申请
            QFontMetrics fontMetrics(m_lf);
            int nFontWidth = fontMetrics.width(strTemp);

            int left = right - 16 - nFontWidth;
            QRect textRect = QRect(left, Y, nFontWidth, lineheight);
            painter->setPen(QPen(crInvalid));
            painter->drawText(textRect, strTemp,
                              QTextOption(Qt::AlignLeft | Qt::AlignVCenter));
            break;
          }
        }

        break;
      }
    }
    if (bValid) Y += lineheight + gap;
  }
}

void CAIPanelDataBusinessTypePanel::DrawButton(QPainter* painter,
                                               QString& szTitle, QRect& R,
                                               int status) {
  QColor ButtonCol;
  if (status == 0) ButtonCol = NAME_COLOR("PartPlate");
  if (status == 1) ButtonCol = NAME_COLOR("ButtonPressed");
  if (status == 2) ButtonCol = NAME_COLOR("Hover");
  if (status == 3) ButtonCol = NAME_COLOR("ButtonNormal");

  painter->setPen(Qt::NoPen);
  painter->setBrush(QBrush(ButtonCol));
  painter->drawRoundedRect(R, 2, 2);

  QColor ButtonText;
  if (status == 0)
    ButtonText = QColor(88, 91, 90);
  else
    ButtonText = NAME_COLOR("TextNormal");
  painter->setPen(QPen(ButtonText));
  QFont font("Microsoft YaHei");
  font.setPixelSize(12);
  painter->setFont(m_lf);

  painter->drawText(R, szTitle,
                    QTextOption(Qt::AlignHCenter | Qt::AlignVCenter));
}

int CAIPanelDataBusinessTypePanel::OnMouseMove(int x, int y) {
  std::map<int, QRect>::iterator it = m_rcButtons.begin();
  QPoint pt(x, y);
  for (; it != m_rcButtons.end(); it++) {
    if (it->second.contains(pt)) {
      if (m_nMousePos != it->first) {
        m_nMousePos = it->first;
        return m_nMousePos;
      } else
        return -1;
    }
  }
  if (m_nMousePos != 0) {
    m_nMousePos = 0;
    return 0;
  }
  return -1;
}

int CAIPanelDataBusinessTypePanel::OnLButtonDown(int x, int y) {
  std::map<int, QRect>::iterator it = m_rcButtons.begin();
  QPoint pt(x, y);
  for (; it != m_rcButtons.end(); it++) {
    std::vector<int>::iterator itr =
        std::find(m_stDisable.begin(), m_stDisable.end(), it->first);
    if (it->second.contains(pt) && itr == m_stDisable.end()) {
      m_nMouseDown = it->first;
      return m_nMouseDown;
    }
  }
  if (m_nMouseDown != 0) {
    m_nMouseDown = 0;
    return 0;
  }
  return -1;
}

int CAIPanelDataBusinessTypePanel::OnLButtonUp(int x, int y) {
  std::map<int, QRect>::iterator it = m_rcButtons.begin();
  QPoint pt(x, y);
  for (; it != m_rcButtons.end(); it++) {
    if (it->second.contains(pt)) {
      std::vector<int>::iterator itr =
          std::find(m_stDisable.begin(), m_stDisable.end(), it->first);
      if (m_nMouseDown == it->first && itr == m_stDisable.end()) {
        if (!m_pParent) {
          m_nMouseDown = 0;
          return it->first;
        }
        int nIndex = m_nMouseDown;  // brokerID;
        CUserAccountInfo& usrInfo =
            SSUserAccountManager::instance().GetUserInfo();

        QBAuthorityApplyReq_t stReq;
        FIELDCOPY(stReq.m_UserId, usrInfo.m_UserId);
        qb::SSRequestMsg msg;

        const CBrokerList& brokerList =
            SSUserAccountManager::instance().GetReadonlyBroker();
        bool bFound = false;
        for (CBrokerList::BrokerMap::const_iterator itor =
                 brokerList.GetBrokerMap().begin();
             !bFound && itor != brokerList.GetBrokerMap().end(); itor++) {
          const CBrokerUnit& stUnit = itor->second;
          if (stUnit.m_brokerID != nIndex) continue;
          if (stUnit.m_BusinessList.size() <= 0) continue;
          CBrokerList::BusinessList::const_iterator itBus =
              stUnit.m_BusinessList.begin();
          for (; !bFound && itBus != stUnit.m_BusinessList.end(); itBus++) {
            if (strcmp(itBus->m_BusinessCode,
                       m_strType.toStdString().c_str()) == 0) {
              FIELDCOPY(stReq.m_BusinessAllowId, itBus->m_BusinessAllowId);
              FIELDCOPY(stReq.m_BrokerID, itBus->m_BrokerID);
              msg.m_pRequest = &stReq;
              msg.m_FuncID = E_FID_QB_AUTHORITY_APPLY_REQ;
              MessagePoster poster(&msg, auth::EncodeFunc);
              MessageReceiver* pmr = dynamic_cast<MessageReceiver*>(m_pParent);
              poster.send(pmr);
              bFound = true;
            }
          }
        }
        m_nMouseDown = 0;
        return it->first;
      } else {
        m_nMouseDown = 0;
        return m_nMouseDown;
      }
    }
  }
  if (m_nMouseDown != 0) {
    m_nMouseDown = 0;
    return m_nMouseDown;
  }

  return -1;
}

///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// class CAIPanelDataBroker
CAIPanelDataBroker::CAIPanelDataBroker() {}

CAIPanelDataBroker::~CAIPanelDataBroker() { ClearPanels(); }

void CAIPanelDataBroker::ClearPanels() {
  std::vector<CAIPanelDataBusinessTypePanel*>::iterator itor =
      m_pPanels.begin();
  for (; itor != m_pPanels.end();) {
    CAIPanelDataBusinessTypePanel* pPanel = *itor;
    if (pPanel) delete pPanel;
    itor = m_pPanels.erase(itor);
  }
}

void CAIPanelDataBroker::Init() {
  ClearPanels();
  int nFontHeight = 24;
  const int gap = 8;

  m_nTitleHeight = nFontHeight;
  m_nHeight = m_nTitleHeight + gap;

  CAIPanelDataBusinessTypePanel* pPanelBOND = new CAIPanelDataBusinessTypePanel(
      CAIPanelDataBusinessTypePanel::typeBond, this);
  pPanelBOND->SetParent(m_pParent);
  pPanelBOND->SetFont(m_lfContent);
  pPanelBOND->Init();
  m_pPanels.push_back(pPanelBOND);
  m_nHeight += pPanelBOND->GetHeight() + gap;

  CAIPanelDataBusinessTypePanel* pPanelIRS = new CAIPanelDataBusinessTypePanel(
      CAIPanelDataBusinessTypePanel::typeIRS, this);
  pPanelIRS->SetParent(m_pParent);
  pPanelIRS->SetFont(m_lfContent);
  pPanelIRS->Init();
  m_pPanels.push_back(pPanelIRS);
  m_nHeight += pPanelIRS->GetHeight() + gap;

  CAIPanelDataBusinessTypePanel* pPanelNCD = new CAIPanelDataBusinessTypePanel(
      CAIPanelDataBusinessTypePanel::typeNCD, this);
  pPanelNCD->SetParent(m_pParent);
  pPanelNCD->SetFont(m_lfContent);
  pPanelNCD->Init();
  m_pPanels.push_back(pPanelNCD);
  m_nHeight += pPanelNCD->GetHeight() + gap;

  CAIPanelDataBusinessTypePanel* pPanelNCD_P =
      new CAIPanelDataBusinessTypePanel(
          CAIPanelDataBusinessTypePanel::typeNCD_P, this);
  pPanelNCD_P->SetParent(m_pParent);
  pPanelNCD_P->SetFont(m_lfContent);
  pPanelNCD_P->Init();
  m_pPanels.push_back(pPanelNCD_P);
  m_nHeight += pPanelNCD_P->GetHeight() + gap;

  CAIPanelDataBusinessTypePanel* pPanelKungfu =
      new CAIPanelDataBusinessTypePanel(
          CAIPanelDataBusinessTypePanel::typeKungfuBond, this);
  pPanelKungfu->SetParent(m_pParent);
  pPanelKungfu->SetFont(m_lfContent);
  pPanelKungfu->Init();
  m_pPanels.push_back(pPanelKungfu);
  m_nHeight += pPanelKungfu->GetHeight() + gap;

  CAIPanelDataBusinessTypePanel* pPanelBill = new CAIPanelDataBusinessTypePanel(
      CAIPanelDataBusinessTypePanel::typeBill, this);
  pPanelBill->SetParent(m_pParent);
  pPanelBill->SetFont(m_lfContent);
  pPanelBill->Init();
  m_pPanels.push_back(pPanelBill);
  m_nHeight += pPanelBill->GetHeight() + gap;

  {
    pPanelBOND->setObjectName("BOND");
    pPanelIRS->setObjectName("IRS");
    pPanelNCD->setObjectName("NCD");
    pPanelKungfu->setObjectName("Kungfu");
    pPanelBill->setObjectName("BILL");
  }
}

void CAIPanelDataBroker::Draw(QPainter* painter, int height) {
  QColor brText = NAME_COLOR("Highlight");
  int nGap = 8;

  // 数据权限-经纪商
  QRect r = rect();
  int X = nGap;
  int Y = 0;

  QString strTemp;
  strTemp = NewStringTable::strAIPanelDataBroker(0);  // 数据权限 - 经纪商
  QRect textRect = QRect(X, Y, r.right() - X, 24);
  painter->setPen(QPen(brText));
  QFont font("Microsoft YaHei");
  font.setPixelSize(16);
  painter->setFont(font);

  painter->drawText(textRect, strTemp,
                    QTextOption(Qt::AlignLeft | Qt::AlignVCenter));
  int nTop = Y + 24 + nGap;
  std::vector<CAIPanelDataBusinessTypePanel*>::iterator itor =
      m_pPanels.begin();
  for (; itor != m_pPanels.end(); itor++) {
    CAIPanelDataBusinessTypePanel* pPanel = *itor;
    if (pPanel) {
      pPanel->move(0, nTop);
      pPanel->SetRect(r);
      pPanel->Draw(painter, nTop);
      nTop += pPanel->GetHeight() + nGap;
    }
  }
}

int CAIPanelDataBroker::OnMouseMove(int x, int y) {
  std::vector<CAIPanelDataBusinessTypePanel*>::iterator it = m_pPanels.begin();
  for (; it != m_pPanels.end(); it++) {
    CAIPanelDataBusinessTypePanel* pPanel = *it;
    if (pPanel) {
      int ret = pPanel->OnMouseMove(x, y);
      if (ret >= 0) {
        return ret;
      }
    }
  }
  return -1;
}

void CAIPanelDataBroker::paintEvent(QPaintEvent* event) {
  QStylePainter painter(this);
  Draw(&painter, 0);
}

void CAIPanelDataBroker::mousePressEvent(QMouseEvent* event) {
  std::vector<CAIPanelDataBusinessTypePanel*>::iterator it = m_pPanels.begin();
  for (; it != m_pPanels.end(); it++) {
    CAIPanelDataBusinessTypePanel* pPanel = *it;
    if (pPanel) {
      int ret = pPanel->OnLButtonDown(event->x(), event->y());
      ret = pPanel->OnLButtonUp(event->x(), event->y());
      if (ret >= 0) {
        return;
      }
    }
  }

  return;
}
